﻿/* ----------------------------------------------------------------------------
 * Copyright (c) Rob Ashton 2009
 *
 * This source code is subject to terms and conditions of the Microsoft Public 
 * License (Ms-PL).
 * 
 * By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 * 
 * To request a copy of the Microsoft Public License, send a request by email
 * to license@mvcex.com
 *
 * This notice must not be removed from this software.
 * ---------------------------------------------------------------------------- */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEx.Configuration;
using MvcEx.Views;

namespace MvcEx
{
    /// <summary>
    /// An implementation of the view engine configuration provider that uses an instance of MvcExApplication to determine
    /// what the current view engine configuration should be
    /// </summary>
    public class MvcExApplicationViewEngineConfigurationSelector : IMvcExViewEngineConfigurationSelector
    {
        private IMvcExApplication mApplication;

        public MvcExApplicationViewEngineConfigurationSelector(IMvcExApplication application)
        {
            mApplication = application;
        }

        #region IMvcExViewEngineConfigurationProvider Members

        public MvcExViewEngineConfiguration GetConfiguration()
        {
            // Create a configuration pointing at the root of the modules directory
            MvcExViewEngineConfiguration configuration = new MvcExViewEngineConfiguration(String.Format("~/{0}/", MvcExConstants.MODULE_DIRECTORY));
            
            // Register each module
            foreach(MvcExConfigurationModule module in mApplication.GetActiveConfiguration().Modules)
            {
                  configuration.RegisterComponent(module.Name);
            }

            // And return the created configuration
            return configuration;
        }

        #endregion
    }
}
